<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="进制转换 首先我们先来了解下进制间是如何转换的。    常用进制 二进制 : 0-1 逢二进一 八进制   0-7 逢八进一 十进制   0-9 逢十进一 十六进制 0-9 A-F表示10-15 逢十六进一  eg：二进制转10进制  所有向十进制转换的都可以采用这样的方式进行计算，二进制以二为底 8进制以8为底，16进制以16为底  十进制转二进制 二进制的加减乘除 加法：  减法：  乘法：">
<meta property="og:type" content="article">
<meta property="og:title" content="java二进制基础知识">
<meta property="og:url" content="https://zhangzt123.github.io/2019/10/03/2019-10-03-java二进制基础知识/index.html">
<meta property="og:site_name">
<meta property="og:description" content="进制转换 首先我们先来了解下进制间是如何转换的。    常用进制 二进制 : 0-1 逢二进一 八进制   0-7 逢八进一 十进制   0-9 逢十进一 十六进制 0-9 A-F表示10-15 逢十六进一  eg：二进制转10进制  所有向十进制转换的都可以采用这样的方式进行计算，二进制以二为底 8进制以8为底，16进制以16为底  十进制转二进制 二进制的加减乘除 加法：  减法：  乘法：">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://zhangzt123.github.io/2019/10/03/2019-10-03-java二进制基础知识/%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%8D%81%E8%BF%9B%E5%88%B6.jpg">
<meta property="og:image" content="https://zhangzt123.github.io/2019/10/03/2019-10-03-java二进制基础知识/%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E4%BA%8C%E8%BF%9B%E5%88%B6.jpg">
<meta property="og:image" content="https://zhangzt123.github.io/2019/10/03/2019-10-03-java二进制基础知识/%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%B4%9F%E6%95%B0.jpg">
<meta property="og:image" content="https://zhangzt123.github.io/2019/10/03/2019-10-03-java二进制基础知识/%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%8A%A0%E6%B3%95.jpg">
<meta property="og:image" content="https://zhangzt123.github.io/2019/10/03/2019-10-03-java二进制基础知识/%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%87%8F%E6%B3%95.jpg">
<meta property="og:updated_time" content="2020-04-20T02:12:10.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="java二进制基础知识">
<meta name="twitter:description" content="进制转换 首先我们先来了解下进制间是如何转换的。    常用进制 二进制 : 0-1 逢二进一 八进制   0-7 逢八进一 十进制   0-9 逢十进一 十六进制 0-9 A-F表示10-15 逢十六进一  eg：二进制转10进制  所有向十进制转换的都可以采用这样的方式进行计算，二进制以二为底 8进制以8为底，16进制以16为底  十进制转二进制 二进制的加减乘除 加法：  减法：  乘法：">
<meta name="twitter:image" content="https://zhangzt123.github.io/2019/10/03/2019-10-03-java二进制基础知识/%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%8D%81%E8%BF%9B%E5%88%B6.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zhangzt123.github.io/2019/10/03/2019-10-03-java二进制基础知识/">





  <title>java二进制基础知识 | </title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title"></span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zhangzt123.github.io/2019/10/03/2019-10-03-java二进制基础知识/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="MR.zhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content>
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">java二进制基础知识</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-03T13:37:43+08:00">
                2019-10-03
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2020-04-20T10:12:10+08:00">
                2020-04-20
              </time>
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/计算机组成/" itemprop="url" rel="index">
                    <span itemprop="name">计算机组成</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="进制转换"><a href="#进制转换" class="headerlink" title="进制转换"></a><strong>进制转换</strong></h1><blockquote>
<p><em>首先我们先来了解下进制间是如何转换的。</em>  </p>
</blockquote>
<h2 id="常用进制"><a href="#常用进制" class="headerlink" title="常用进制"></a><strong>常用进制</strong></h2><ul>
<li>二进制 : 0-1 逢二进一</li>
<li>八进制   0-7 逢八进一</li>
<li>十进制   0-9 逢十进一</li>
<li>十六进制 0-9 A-F表示10-15 逢十六进一</li>
</ul>
<h2 id="eg：二进制转10进制"><a href="#eg：二进制转10进制" class="headerlink" title="eg：二进制转10进制"></a><strong>eg：二进制转10进制</strong></h2><p><img src="/2019/10/03/2019-10-03-java二进制基础知识/%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%8D%81%E8%BF%9B%E5%88%B6.jpg" alt=" "></p>
<blockquote>
<p><em>所有向十进制转换的都可以采用这样的方式进行计算，二进制以二为底 8进制以8为底，16进制以16为底</em></p>
</blockquote>
<h2 id="十进制转二进制"><a href="#十进制转二进制" class="headerlink" title="十进制转二进制"></a><strong>十进制转二进制</strong></h2><p><img src="/2019/10/03/2019-10-03-java二进制基础知识/%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E4%BA%8C%E8%BF%9B%E5%88%B6.jpg" alt=" "><br><img src="/2019/10/03/2019-10-03-java二进制基础知识/%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%B4%9F%E6%95%B0.jpg" alt=" "></p>
<h2 id="二进制的加减乘除"><a href="#二进制的加减乘除" class="headerlink" title="二进制的加减乘除"></a><strong>二进制的加减乘除</strong></h2><ul>
<li><p><strong>加法：</strong><br><img src="/2019/10/03/2019-10-03-java二进制基础知识/%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%8A%A0%E6%B3%95.jpg" alt=" "></p>
</li>
<li><p><strong>减法：</strong><br><img src="/2019/10/03/2019-10-03-java二进制基础知识/%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%87%8F%E6%B3%95.jpg" alt=" "></p>
</li>
<li><p><strong>乘法：</strong></p>
</li>
<li><p><strong>除法：</strong></p>
<blockquote>
<p>乘法除法暂未研究,后续会进行补充<br>了解了这么多我们开始java的位运算吧。</p>
</blockquote>
</li>
</ul>
<h1 id="JAVA位运算"><a href="#JAVA位运算" class="headerlink" title="JAVA位运算"></a><strong>JAVA位运算</strong></h1><blockquote>
<p><em>java中,位运算包括 ^（亦或）, &amp;（与），|（或） ,&lt;&lt;（左移）,&gt;&gt;（右移）,&lt;&lt;&lt;（无符号左移）,&gt;&gt;&gt;（无符号右移）在继续往下看之前，我们还要了解什么是机器码，由于计算机只能识别0和1， 所有在计算机中用0和1表示正负，这就是符号位（最高位表示），符号位连同数据一起代码化的就是机器码。机器码加绝对值表示的数值就是真值。机器码分有符号数和无符号数，无符号数由所有数值表示。</em></p>
</blockquote>
<blockquote>
<p><em>计算机系统中，数值一律用补码来表示：因为补码可以使符号位和数值位统一处理，同时可以使<strong>减法按照加法来处理</strong>。数值编码分为原码，反码，补码，<strong>符号位（最高位）均为0正1负</strong>。</em><br><em>原码表示最接近真值 ，规定最高位为符号位 0+ 1- 数值在符号位后面以绝对值给出</em><br><em>补码表示就是将符号位作为数值参与运算，并使所有加减以均以加法代替。<strong>负数的补码是原码取反加一，正数补码是它本身。*</strong><br>*反码是一种特殊的补码，反码的模比补码最低位小1</em></p>
</blockquote>
<ul>
<li><strong>原码 -&gt; 补码</strong>： 数值位取反加1.</li>
<li><strong>补码 -&gt; 原码</strong>： 对该补码的数值位 取反 加1.</li>
<li><strong>补码 的绝对值</strong>（称为真值）：正数的真值就是本身，负数的真值是各位（包括符号位）取反加1（即变成原码并把符号位取反）.</li>
</ul>
<hr>
<blockquote>
<p>NOTE* 补码”或”运算之后为补码</p>
</blockquote>
<h2 id="亦或"><a href="#亦或" class="headerlink" title="^(亦或)"></a><strong>^(亦或)</strong></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> a=-<span class="number">14</span>;<span class="comment">// 0000 1110</span></span><br><span class="line">		<span class="keyword">int</span> b=-<span class="number">9</span>;<span class="comment">//  0000 1001</span></span><br><span class="line"><span class="comment">/*相同为0，不同为1 */</span></span><br><span class="line"><span class="comment">/*     符号位（0）                       | 符号位（1）             14         取反       加一</span></span><br><span class="line"><span class="comment">*      0000  1110   14                  |      1111 0010    -14 补码 0000 1110-&gt;1111 0001-&gt;1111 0010</span></span><br><span class="line"><span class="comment">*   ^  0000  1001   9                   |  ^   1111 0111     -9 补码 0000 1001-&gt;1111 0110-&gt;1111 0111</span></span><br><span class="line"><span class="comment">* ---------------                       | -----------------    补码"异或"运算之后为补码</span></span><br><span class="line"><span class="comment">*      0000  0111 补码-&gt;源码 0000  0111  |      0000 0101 补码-&gt;源码  0000 0101</span></span><br><span class="line"><span class="comment">*      1*2^2+1*2^1+1*2^0=7              | 1*2^2+1*2^0 =5    结果 5</span></span><br><span class="line"><span class="comment">* */</span></span><br><span class="line">		System.out.println(<span class="string">"a与b:"</span>+String.valueOf(a^b));</span><br><span class="line"></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h2 id="amp-与"><a href="#amp-与" class="headerlink" title="&amp;(与)"></a><strong>&amp;(与)</strong></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> a=-<span class="number">1</span>;<span class="comment">//0000 0001 -&gt;1111 1110-&gt;1111 1111 负数 补符号位（符号位不变）1  取反加一</span></span><br><span class="line">		<span class="keyword">int</span> b=<span class="number">10</span>;<span class="comment">//0000 1010 符号位0</span></span><br><span class="line"><span class="comment">/*同1为1，否则为0*/</span></span><br><span class="line"><span class="comment">/*                            |</span></span><br><span class="line"><span class="comment">*      1111 1111  -1          |      1111 0010    -14 补码 0000 1110-&gt;1111 0001-&gt;1111 0010</span></span><br><span class="line"><span class="comment">*   &amp;  0000 1010  10          |   &amp;  1111 0111     -9 补码 0000 1001-&gt;1111 0110-&gt;1111 0111</span></span><br><span class="line"><span class="comment">* --------------              | ----------------</span></span><br><span class="line"><span class="comment">*      0000 1010 -&gt;0000 1010  |      1111 0010  -&gt;  1000 1101-&gt;1000 1110</span></span><br><span class="line"><span class="comment">*      1*2^3+1*2^1=10         |      1*2^3 +1*2^2+1*2^1= -14</span></span><br><span class="line"><span class="comment">*      最高为是0,不用转化             如果最高位为1 即负数 最高位不变 其他位取反，加一 </span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* */</span></span><br><span class="line">		System.out.println(<span class="string">"a与b:"</span>+String.valueOf(a&amp;b));</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h2 id="或"><a href="#或" class="headerlink" title="|(或)"></a><strong>|(或)</strong></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> a=<span class="number">14</span>;<span class="comment">// 0000 1110</span></span><br><span class="line">		<span class="keyword">int</span> b=<span class="number">9</span>;<span class="comment">//  0000 1001</span></span><br><span class="line"><span class="comment">/*有1为1，否则为0 */</span></span><br><span class="line"><span class="comment">/*     符号位（0）                  |     符号位（1）                   负数最高位为1 其他位取反加一</span></span><br><span class="line"><span class="comment">*      0000  1110   14             |      1111 0010    -14 补码0000 1110-&gt;1111 0001-&gt;1111 0010</span></span><br><span class="line"><span class="comment">*   |  0000  1001   9              |  |   0000 1001     9  补码</span></span><br><span class="line"><span class="comment">* ---------------                  | -----------------    </span></span><br><span class="line"><span class="comment">*      0000  1111 -&gt;  0000  1111   |      1111 1011   -&gt;1000 0100 -&gt; 1000 0101 </span></span><br><span class="line"><span class="comment">*                                  |      补码转原码取反加一</span></span><br><span class="line"><span class="comment">*      1*2^3+1*2^2+1*2^1+1*2^0=15  |         1*2^2+1*2^0 =5   符号位为负 结果-5</span></span><br><span class="line"><span class="comment">* */</span></span><br><span class="line">		System.out.println(<span class="string">"a与b:"</span>+String.valueOf(a|b));</span><br><span class="line"></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h2 id="lt-lt-左移"><a href="#lt-lt-左移" class="headerlink" title="&lt;&lt;(左移)"></a><strong>&lt;&lt;(左移)</strong></h2><blockquote>
<p>左移位运算符为«，其运算规则是：按二进制形式把所有的数字向左移动对应的位数，高位移出（舍弃），低位的空位补零。</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> a=-<span class="number">14</span>;<span class="comment">// 0000 1110</span></span><br><span class="line">		<span class="keyword">int</span> b=<span class="number">11</span>; <span class="comment">// 0000 1011</span></span><br><span class="line">		<span class="comment">/*                   |符号位（1）取反 | 加一</span></span><br><span class="line"><span class="comment">		*        0000 1110 -&gt;| 1111 0001 -&gt;  | 1111 0010</span></span><br><span class="line"><span class="comment">		*        &lt;&lt; 左移1  (1)舍去  1110 0100    左移后的补码 正数最高位补0，负数最高位补1</span></span><br><span class="line"><span class="comment">		*                          1001 1011    符号位不变 取反</span></span><br><span class="line"><span class="comment">		*                          1001 1100</span></span><br><span class="line"><span class="comment">		*                          -  1*2^2+1*2^3+1*2^4= 4+8+16=28</span></span><br><span class="line"><span class="comment">		*                  符号位为负 结果负28</span></span><br><span class="line"><span class="comment">		*---------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">		*                     0 000 1011</span></span><br><span class="line"><span class="comment">		*        &lt;&lt; 左移1     0 000 1011 0 正数补0   正数的补码和原码相同</span></span><br><span class="line"><span class="comment">		*                     + 1*2^4+1*2^2+1*2^1= 22</span></span><br><span class="line"><span class="comment">		*</span></span><br><span class="line"><span class="comment">		* */</span></span><br><span class="line">		System.out.println(<span class="string">"a与b:"</span>+String.valueOf(a&lt;&lt;<span class="number">1</span>));</span><br><span class="line">		System.out.println(<span class="string">"a与b:"</span>+String.valueOf(b&lt;&lt;<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h2 id="gt-gt-右移"><a href="#gt-gt-右移" class="headerlink" title="&gt;&gt;(右移)"></a><strong>&gt;&gt;(右移)</strong></h2><blockquote>
<p>右位移运算符为»，其运算规则是：按二进制形式把所有的数字向右移动对应的位数，低位移出（舍弃），高位的空位补零。</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> a=-<span class="number">14</span>;<span class="comment">// 0000 1110</span></span><br><span class="line">		<span class="keyword">int</span> b=<span class="number">11</span>; <span class="comment">// 0000 1011</span></span><br><span class="line">		<span class="comment">/*                   |符号位（1）取反 | 加一</span></span><br><span class="line"><span class="comment">		*        0000 1110 -&gt;| 1111 0001 -&gt;  | 1111 0010</span></span><br><span class="line"><span class="comment">		*        &gt;&gt; 右移3  111 1111 0(010)舍去    左移后的补码 正数最高位补0，负数最高位补1</span></span><br><span class="line"><span class="comment">		*                  100 0000 1   符号位不变 取反</span></span><br><span class="line"><span class="comment">		*                  100 0001 0    +1</span></span><br><span class="line"><span class="comment">		*                  -     1*2^1=2</span></span><br><span class="line"><span class="comment">		*                  符号位为负 结果负2</span></span><br><span class="line"><span class="comment">		*                  右移与左移不同会把移出去的舍掉</span></span><br><span class="line"><span class="comment">		*---------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">		*                     0 000 1011</span></span><br><span class="line"><span class="comment">		*        &gt;&gt; 右移3     0 000 0001（011）  正数补0   正数的补码和原码相同</span></span><br><span class="line"><span class="comment">		*                     + 1*2^0= 1</span></span><br><span class="line"><span class="comment">		*</span></span><br><span class="line"><span class="comment">		* */</span></span><br><span class="line">		System.out.println(<span class="string">"a与b:"</span>+String.valueOf(a&gt;&gt;<span class="number">3</span>));</span><br><span class="line">		System.out.println(<span class="string">"a与b:"</span>+String.valueOf(b&gt;&gt;<span class="number">3</span>));</span><br><span class="line"></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h2 id="（取反）"><a href="#（取反）" class="headerlink" title="~（取反）"></a><strong>~（取反）</strong></h2><p>取反就是1为0,0为1</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> a=~-<span class="number">14</span>;<span class="comment">// 0000 1110</span></span><br><span class="line">		<span class="keyword">int</span> b=~<span class="number">11</span>; <span class="comment">// 0000 1011</span></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		*     0000 1110 --&gt;1111 0001--&gt;1111 0010</span></span><br><span class="line"><span class="comment">		*     取反后 ---&gt; 0 000 1101  13</span></span><br><span class="line"><span class="comment">		*----------------------------------------------------------------</span></span><br><span class="line"><span class="comment">		*     0000 1011</span></span><br><span class="line"><span class="comment">		*     取反后 ---&gt; 1 111 0100 (最高位为1)负数 负数都是补码表示，所有要转换为原码</span></span><br><span class="line"><span class="comment">		*     转换后 ---&gt; 1 000 1100 --&gt; -12</span></span><br><span class="line"><span class="comment">		*</span></span><br><span class="line"><span class="comment">		* */</span></span><br><span class="line">		System.out.println(<span class="string">"a与b:"</span>+String.valueOf(a));</span><br><span class="line">		System.out.println(<span class="string">"a与b:"</span>+String.valueOf(b));</span><br><span class="line"></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h2 id="gt-gt-gt-无符号右移"><a href="#gt-gt-gt-无符号右移" class="headerlink" title="&gt;&gt;&gt;(无符号右移)"></a><strong>&gt;&gt;&gt;(无符号右移)</strong></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> a = -<span class="number">14</span>;<span class="comment">// 0000 1110-&gt;1111 0010</span></span><br><span class="line">		<span class="keyword">int</span> b=<span class="number">11</span>; <span class="comment">// 0000 1011</span></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		*无符号右移中正数和负数计算方式不同，对于正数，不会改变成负数，对于负数，将会变成正数。</span></span><br><span class="line"><span class="comment">		* --------------------------------------------------</span></span><br><span class="line"><span class="comment">		* 负数无符号右移</span></span><br><span class="line"><span class="comment">		*   11111111111111111111111111110010</span></span><br><span class="line"><span class="comment">		*   00011111111111111111111111111110(010)舍去</span></span><br><span class="line"><span class="comment">		*      536870910</span></span><br><span class="line"><span class="comment">		* --------------------------------------------------</span></span><br><span class="line"><span class="comment">		* 正数无符号右移</span></span><br><span class="line"><span class="comment">		*  00000000000000000000000000001011</span></span><br><span class="line"><span class="comment">		*     00000000000000000000000000001(011)舍去</span></span><br><span class="line"><span class="comment">		*     1</span></span><br><span class="line"><span class="comment">		* */</span></span><br><span class="line">		System.out.println(<span class="string">"a无符号右移3:"</span>+String.valueOf(a&gt;&gt;&gt;<span class="number">3</span>));</span><br><span class="line">		System.out.println(<span class="string">"b无符号右移3:"</span>+String.valueOf(b&gt;&gt;&gt;<span class="number">3</span>));</span><br><span class="line"></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<hr>
<blockquote>
<p><strong>*NOTE:</strong> 遇到&amp;= 、|= 、 ^= 、&gt;&gt;= 、&gt;&gt;&gt;=、 &lt;&lt;=  这样的他们表示把计算后的变量重新赋值给变量，就和a+=1 表示 a=a+1 一样。<br><strong>*NOTE:</strong> 为何无符号位移时有32位呢，请看下表    </p>
</blockquote>
<hr>
<!--
| 数据结构   | 字节  | 范围                                        |
| :------   |:------|:--------------------------------------------|
|boolean    |1       |true或false                                   |
|char        |2       |从字符型对应的整型数来划分，其表示范围是0～65535  | 
|byte        |1       |-128～127                                     |
|short        |2       |-32768～32767                                 |
|int        |4       |-2147483648～2147483647 (-2^31 ~ 2^31-1)      |
|long        |8       |-9223372036854775808 ~ 9223372036854775807    |
|float        |4       |-3.4E38～3.4E38                               |
|double        |8       |-1.7E308～1.7E308                             |
-->
<table border="1">
<thead>
<tr>
    <td width="100px">数据结构</td>
    <td width="50px">字节</td>
    <td>范围</td>
</tr>
</thead>
<tbody>

</tbody>
<tr>
    <td>boolean</td>
    <td>1</td>
    <td>true或false</td>
</tr>
<tr>
    <td>char</td>
    <td>2</td>
    <td>从字符型对应的整型数来划分，其表示范围是0～65535</td>
</tr>
<tr>
    <td>byte</td>
    <td>1</td>
    <td>-128～127 </td>
</tr>
<tr>
    <td>short</td>
    <td>2</td>
    <td>-32768～32767  </td>
</tr>
<tr>
    <td>int</td>
    <td>4</td>
    <td>-2147483648～2147483647 (-2^31 ~ 2^31-1)</td>
</tr>
<tr>
    <td>long</td>
    <td>8</td>
    <td>-9223372036854775808 ~ 9223372036854775807</td>
</tr>
<tr>
    <td>float</td>
    <td>4</td>
    <td>-3.4E38～3.4E38 </td>
</tr>
<tr>
    <td>double</td>
    <td>8</td>
    <td>-1.7E308～1.7E308</td>
</tr>
</table>


<hr>
<!-- > *如有问题，请联系作者。* -->
      
    </div>
    
    
    
+ <div style="text-align:center;color: #ccc;font-size:14px;">---------------- The End ----------------</div>
    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/10/02/2019-10-01-从零开始的高并发之路(一)/" rel="next" title="从零开始的高并发之路(一)">
                <i class="fa fa-chevron-left"></i> 从零开始的高并发之路(一)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/10/10/2019-10-10-红黑树算法/" rel="prev" title="红黑树算法">
                红黑树算法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">MR.zhang</p>
              <p class="site-description motion-element" itemprop="description">坚持是一种态度,每一分都值得</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">39</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">23</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#进制转换"><span class="nav-number">1.</span> <span class="nav-text">进制转换</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#常用进制"><span class="nav-number">1.1.</span> <span class="nav-text">常用进制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#eg：二进制转10进制"><span class="nav-number">1.2.</span> <span class="nav-text">eg：二进制转10进制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#十进制转二进制"><span class="nav-number">1.3.</span> <span class="nav-text">十进制转二进制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二进制的加减乘除"><span class="nav-number">1.4.</span> <span class="nav-text">二进制的加减乘除</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#JAVA位运算"><span class="nav-number">2.</span> <span class="nav-text">JAVA位运算</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#亦或"><span class="nav-number">2.1.</span> <span class="nav-text">^(亦或)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#amp-与"><span class="nav-number">2.2.</span> <span class="nav-text">&amp;(与)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#或"><span class="nav-number">2.3.</span> <span class="nav-text">|(或)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#lt-lt-左移"><span class="nav-number">2.4.</span> <span class="nav-text">&lt;&lt;(左移)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#gt-gt-右移"><span class="nav-number">2.5.</span> <span class="nav-text">&gt;&gt;(右移)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#（取反）"><span class="nav-number">2.6.</span> <span class="nav-text">~（取反）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#gt-gt-gt-无符号右移"><span class="nav-number">2.7.</span> <span class="nav-text">&gt;&gt;&gt;(无符号右移)</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">MR.zhang</span>

  
</div>









        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
